/*
CALCULATING TENURE FOR ALL WORKERS IN RAMS

Data 	: DS_Lev_Jobb2000.dta  to  DS_Lev_Jobb2013.dta
Folder 	: rawdata/dtafiles/DS_Lev_Jobb2000.dta
Date	: 2016-12-06

Creator		: Jonas Cederlof (JC)
Description 	: Appending the years 1985-2018 of Jobb data. This is done ONLY 
		  for indiviuals who are present in the notification data
	
Notes		: 36 indivuals not found in Jobb data (see note at end of .do-file).
LATEST UPDATE 	: 2021-10-06
*/

********************************************************************************
clear
set more		 off
cap   log close 	_all
log using 	"../log/A0_append_RAMS.log", replace 

{ // Appending all years of Jobb data (1985 to 2018)
*===============================================================================

*Open 1985 Jobb data
use 	lopnr lopnr_peorgnr year manfran mantill 	using 	"$rawdatapath/jobb_1985.dta", clear


*Appending 1986 to 2013 for indivuals present in displacement data
forvalues i = 1986(1)2018 {
	append using "$rawdatapath/jobb_`i'.dta", keep(lopnr lopnr_peorgnr year manfran mantill)

}
}
*

*Rename variables
rename lopnr	 	 	persid
rename lopnr_peorgnr 	 	firmid
rename manfran 			fi_month		 			
rename mantill 			la_month


{ // Correcting variabels fi_month and la_month
********************************************************************************
* There is a problem with observations where indiviuals are registered as having
* started work at month 0 and ended work month 0. For self-emplyed (yrkstallnky==2)
* this is always the case by construction of the data. As the other guys, they 
* have positive earnings and paid taxes so I recon that this is a meassurement 
* error that is to be considered as a missing value which occurs  in about 1.3% 
* of all yearly spells. (The meassurement error is smaller for later years). 

*The month zero are alwyas occuring jointly
tab 	 la_month  	   fi_month 	 ,missing

{ 	// Look for a pattern for when a "zero-year" happens ***
	*===================================================================
	*Generate: tag "zero-year" observations 
	bys persid firmid  year  : gen  tag = 1 if fi_month==0 & la_month==0

	*Generate: Are the "zero-year" followed or precluded by a full year spell?
	bys persid firmid (year) : gen  diffmonth  = la_month + la_month[_n+1] if tag[_n]==1
	bys persid firmid (year) : gen  diffmonth2 = la_month + la_month[_n-1] if tag[_n]==1

	tab2 diffmonth*
	tab  diffmonth
}		
* Conclusion: In almost all cases these occur either in pairs (several 
* zero-years in a row) or they are followed or precluded by a yearly 
* employment spell that has lasted until month 12. Thus I assume that 
* these yearly spells are to be full-time spells. (This is also what 
* you kind of see when you eyeball the data). Thus I replace these 
* missing observations with fi_month==1 and la_month==12. These obs- 
* ervations are also flagged at the firm level(!) so that the robust-
* ness to this assumption can be evaluated.

*Flag: flag emp. spells that we've changed from month 0 to either 1 or 12
gen 	 temp = la_month==0 |la_month==99 | fi_month==0 | fi_month==99 
fastmax  temp , by(persid firmid) name(flag_work)
lab var  flag_work 	"Flag: ==1 if 'missing' months observations in work register"

*Replace: since they are always joinlty defined as 0 I only need one if-restriction
replace fi_month = 1 if fi_month==0
replace fi_month = 1 if fi_month==99

replace la_month = 12 if la_month==0 
replace la_month = 12 if la_month==99
drop diff* tag temp

}
*

*Firm tenure in months worked
forvalues m = 1(1)12 {
	gen 	work`m' =  fi_month<=`m' & la_month>=`m'
}

gcollapse (max) work* la_month (min) fi_month , by(persid firmid year)

*Calculate tenure by end of year
egen Myear = rowtotal(work*)
bys persid firmid (year) : gen tenure_all = sum(Myear)

*Label
lab var tenure_all 	"Tenure at the end of the year (summed months of work by firm)"

*Keep variables
keep persid year firmid tenure_all

*Save
compress
save "$datapath/A0_tenure_for_all.dta",replace


